package custom.base.ui.activity;

import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import custom.base.R;
import custom.base.R2;
import custom.base.data.Global;
import custom.base.utils.AppUtils;
import custom.base.utils.TxtUtil;

public abstract class CrashActivity extends BaseActivity {
    @BindView(R2.id.act_crash_restart)
    TextView tvRestart;
    @BindView(R2.id.act_crash_back)
    TextView tvBack;
    @BindView(R2.id.act_crash_content)
    TextView tvContent;
    @BindView(R2.id.act_crash_hint)
    TextView tvHint;
    @BindView(R2.id.act_crash_update)
    TextView tvUpdate;

    protected String content;
    /**
     * 根包名相关的重点字符行
     */
    private List<String> pointStrList = new ArrayList<>();

    @Override
    public int getLayoutID() {
        return R.layout.act_crash;
    }

    @Override
    public void initView(@NonNull Bundle savedInstanceState) {
        super.initView(savedInstanceState);
//        tvRestart = ButterKnife.findById(this, R.id.act_crash_restart);
//        tvBack = ButterKnife.findById(this, R.id.act_crash_back);
//        tvContent = ButterKnife.findById(this, R.id.act_crash_content);
//        tvHint = ButterKnife.findById(this, R.id.act_crash_hint);
//        tvUpdate = ButterKnife.findById(this, R.id.act_crash_update);
    }

    @Override
    public void initObject(@NonNull Bundle savedInstanceState) {
        super.initObject(savedInstanceState);
        content = getIntent().getStringExtra("content");
    }

    @Override
    public void initData(@NonNull Bundle savedInstanceState) {
        super.initData(savedInstanceState);

        tvHint.setText("“" + AppUtils.getAppName(this) + "”发生未知错误，已停止运行\n版本：" + AppUtils.getVersionName(this) + " 系统版本：" + AppUtils.getSystemVersion()
                + "\n 手机名称：" + AppUtils.getVendor() + " 手机型号：" + AppUtils.getSystemModel());
        if (!TxtUtil.isEmpty(content)) {
            analyzeSting(content);
        } else {
            tvContent.setText("无错误信息");
        }
        if (Global.getInstance().isDebugMode()) {
            tvBack.setVisibility(View.VISIBLE);
        } else {
            tvBack.setVisibility(View.GONE);

            AppManager.getInstance().finishAllActivityExcept(this);//如果是发布模式的话先关闭所有的acitivity
        }

    }

    @Override
    public void initListener() {
        tvRestart.setOnClickListener(this);
        tvBack.setOnClickListener(this);
        tvUpdate.setOnClickListener(this);
    }

    /**
     * 把异常信息按照每行拆看，得到需要重点展示的字符
     */
    private void analyzeSting(String exceptionStr) {
        String[] tmp = exceptionStr.split("\n");
        for (int i = 0; i < tmp.length; i++) {
            if (i < 2)//前两行也默认重点展示
                pointStrList.add(tmp[i]);
        }

        SpannableString ss = new SpannableString(exceptionStr);
        for (int i = 0; i < pointStrList.size(); i++) {//前面两行标蓝
            int spanStart;
            int spanEnd;
            spanStart = content.indexOf(pointStrList.get(i));
            spanEnd = spanStart + pointStrList.get(i).length();
            ss.setSpan(new ForegroundColorSpan(ContextCompat.getColor(this, R.color.app_btn_blue))
                    , spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        pointStrList.clear();
        for (int i = 0; i < tmp.length; i++) {//提取出重点文本
            if (tmp[i].contains(getPackageName())) {
                pointStrList.add(tmp[i]);
            }
        }

        for (int i = 0; i < pointStrList.size(); i++) {//重点文本标红
            int spanStart;
            int spanEnd;
            spanStart = content.indexOf(pointStrList.get(i));
            spanEnd = spanStart + pointStrList.get(i).length();
            ss.setSpan(new ForegroundColorSpan(ContextCompat.getColor(this, R.color.app_btn_red))
                    , spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        tvContent.setText(ss);
    }

    @Override
    public void onClick(View v, int clickID) {
        if (clickID == tvRestart.getId()) {
            onRestartClick();
        } else if (clickID == tvBack.getId()) {
            onBackClick();
        } else if (clickID == tvUpdate.getId()) {
            onUpdateClick();
        }
    }

    public void onRestartClick() {
        if (Global.getInstance().isDebugMode()) {
            AppManager.getInstance().finishAllActivity();
        }
    }

    public void onBackClick() {
        finish();
        overridePendingTransition(R.anim.keep, R.anim.custom_fade_out);
    }

    public void onUpdateClick() {

    }
}
